package com.example.easydownloadback.config;

import com.example.easydownloadback.interceptor.LoginInterceptor;
import com.example.easydownloadback.interceptor.RefreshTokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截所有请求，负责刷新用户登录状态，存储从Redis获取的用户数据
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
                .addPathPatterns("/admin/**").order(0);
        // 拦截需要登录的请求，负责验证当前是否有用户登录
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/admin/**")
                .excludePathPatterns(
                        "/admin/user/login",
                        "/admin/file/upload/**",
                        "/admin/file/download/**"
                ).order(1);
    }
}
